package com.oriental.manage.controller.specially;

import com.oriental.manage.core.exception.BusiException;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;

import com.oriental.manage.core.utils.DateUtils;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.core.utils.Validate;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.pojo.specially.FindBatchInfo;
import com.oriental.manage.pojo.specially.PersonRiskInfo;
import com.oriental.manage.service.redis.RedisService;
import com.oriental.manage.service.specially.BatchMessageService;
import com.oriental.manage.service.specially.PersonRiskInfoService;
import com.oriental.paycenter.commons.utils.DateUtil;
import com.oriental.paycenter.commons.utils.RandomMath;
import com.oriental.riskfront.api.enums.TransCode;
import com.oriental.riskfront.api.model.*;
import com.oriental.riskfront.api.model.specially.ResponseModel;
import com.oriental.riskfront.api.service.RiskBatchInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * Created by luoxin on 2017/4/20.
 */
@Slf4j
@Controller
@RequestMapping("riskInfo")
public class PersonRiskInfoReportController {



@SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private PersonRiskInfoService personRiskInfoService;
    @Autowired
    private BatchMessageService batchMessageService;
    @Autowired
    protected RedisService redisService;

    @Autowired
    private RiskBatchInterface riskBatchInterface;

    @RequestMapping("personRiskInfo/init")
    public String init() {

        return "specially/searchPersonRiskInfo";
    }

    @RequestMapping("personRiskInfo/search")
    @ResponseBody
    public ResponseDTO<Pagination<PersonRiskInfo>> queryPage(Pagination<PersonRiskInfo> pagination, PersonRiskInfo userInfo) {
        ResponseDTO<Pagination<PersonRiskInfo>> responseDTO = new ResponseDTO<Pagination<PersonRiskInfo>>();
        try {
            log.info("查询个人风险信息请求参数,userInfo:{}",userInfo);
            personRiskInfoService.queryPage(pagination, userInfo);
            responseDTO.setSuccess(true);
            log.info("查询个人风险信息返回结果,pagination:{}",pagination);
            responseDTO.setObject(pagination);
        } catch (Exception e) {
            log.error("查询失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @RequestMapping("personRiskInfo/toAdd")
    public String add() {
        return "specially/addPersonRiskInfo";
    }

    @RequestMapping("personRiskInfo/toDetail")
    public String queryDetail() {
        return "specially/personRiskInfoDetail";
    }

    @RequestMapping("personRiskInfo/add")
    @ResponseBody
    public ResponseDTO<String> addPersonRiskInfo(@RequestBody PersonRiskInfo personRiskInfo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        BatchMessage batchMessage = new BatchMessage();
        try {//Z2012931000018
            String areaCode=personRiskInfo.getOccurarea();
            if(areaCode.length()>1){
                String code=areaCode.substring(0,1);
                if(",".equals(code)){
                    personRiskInfo.setOccurarea(areaCode.substring(1));
                }
            }else{
                personRiskInfo.setOccurarea("");
            }
            String Id=DateUtil.getCurrent().concat(RandomMath.getNum(5));
           String user=SessionUtils.getUserName();
            personRiskInfo.setId(Id);
            personRiskInfo.setOrgId("Z2012931000018");
            personRiskInfo.setCusProperty("01");
            personRiskInfo.setRepType("03");
            personRiskInfo.setUpdateType("00");//00新增
            personRiskInfo.setStatus("00");//初始状态
            personRiskInfo.setRepPerson(user);
            batchMessage.setId(DateUtil.getCurrent().concat(RandomMath.getNum(5)));
            batchMessage.setSubmitMsgId(Id);
            batchMessage.setStatus("00");
            batchMessage.setDeleteFlag("0");
            batchMessage.setCount(1);
            batchMessage.setTrnxCode(TransCode.PR0001.getCode());
            batchMessage.setReportDate(new Date());
            batchMessage.setCreateBy(user);
            batchMessage.setDocCode(personRiskInfo.getDocCode());
            batchMessage.setRiskType(personRiskInfo.getRiskType());
            batchMessage.setLevel(personRiskInfo.getLevel());
            log.info("新增个人风险信息,personRiskInfo:{}",personRiskInfo);
            personRiskInfoService.insert(personRiskInfo, responseDTO);
            batchMessageService.insertBatchMessage(batchMessage);
        } catch (Exception e) {
            log.error("新增失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }


 @RequestMapping("personRiskInfo/delete")
    @ResponseBody
    public ResponseDTO<String> delete(PersonRiskInfo personRiskInfo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<String>();
        try {
            personRiskInfoService.delete(personRiskInfo, responseDTO);
        } catch (Exception e) {
            log.error("删除失败", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    //-------------------


    /**
     *批量查询入口
     * @return
     */
    @RequestMapping("batchInfoImportQuery/init")
    public String batchInfoImportQuery(){

        return "specially/commBatch";
    }

    @RequestMapping("batchInfoImportQuery/batchList")
    public String batchList(){

        return "specially/batchList";
    }
    @RequestMapping("batchInfoImportQuery/batchDetailList")
    public String batchDetailList(){

        return "specially/batchDetailList";
    }
    /**
     * 批量查询数据
     */
    @RequestMapping("batchInfoImportQuery/findList")
    @ResponseBody
    public ResponseDTO<List<Object>> findList(@RequestBody FindBatchInfo findBatchInfo){
        log.info("批量查询数据--------------------------");
        ResponseDTO<List<Object>>  responseDTO= new ResponseDTO<List<Object>>();
        //组装查询数据
        FindCollectInfo findCollect = new FindCollectInfo();
        findCollect.setCusProperty(findBatchInfo.getCusProperty());
        String Infos = findBatchInfo.getInfos();
        Infos = Infos.replace("，",",");
        findCollect.setInfos(Infos);
        findCollect.setKeyWord(findBatchInfo.getKeyWord());
        findBatchInfo.setInfo("");
        findBatchInfo.setRiskType("");
        try{
               Validate.validateObject(findBatchInfo);
               //调用接口请求数据
               ResponseModel response= riskBatchInterface.batchInfoImportQuery(findCollect);
               boolean bool = response.isSuccess();
               if(!bool){
                   log.error(response.getErrorMsg());
                   responseDTO.setSuccess(false);
                   responseDTO.setMsg(response.getErrorMsg());
                   return responseDTO;
               }
               //个人/商户
               if(findBatchInfo.getCusProperty().equals("01")){
                   PersonalRiskList personalRiskList= (PersonalRiskList)response.getResult();
                   List<PersonalRiskBody> list = personalRiskList.getPersonalRisk();
                   responseDTO.setSumObject(list);
               }else {
                   CommercialRiskList commercialRiskList= (CommercialRiskList)response.getResult();
                   List<CommercialRiskBody> list = commercialRiskList.getCommercialRisk();
                   responseDTO.setSumObject(list);
               }
               responseDTO.setSuccess(true);
               /*int pageIndex= Integer.parseInt(findBatchInfo.getPageIndex()) ;
               int pageSize = Integer.parseInt(findBatchInfo.getPageSize())<=list.size()?Integer.parseInt(findBatchInfo.getPageSize()):list.size() ;

               list = list.subList((pageIndex-1)*pageSize,(pageIndex-1)*pageSize+pageSize);//第几个到第几个 start,end.start = (pageIndex-1)*pageSize , end = (pageIndex-1)*pageSize+pageSize*/

        }catch (BusiException e){
            log.error("查询参数格式不正确！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getDesc());
        }catch (Exception e){
            log.error("查询批量数据失败！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询批量数据失败");
        }
        return responseDTO;
    }

    /**
     * 汇总查询入口
     * @return
     */
    @RequestMapping("riskInfoSummaryQuery/init")
    public String riskInfoSummaryQueryInit(){

        return "specially/commCollect";
    }

    /**
     * 汇总查询数据
     * @return
     */
    @RequestMapping("riskInfoSummaryQuery/riskQueryInfo")
    @ResponseBody
    public synchronized ResponseDTO<List<Object>> riskQueryInfo(@RequestBody FindBatchInfo findBatchInfo){
        log.info("汇总查询数据");
        ResponseDTO<List<Object>> responseDTO = new ResponseDTO<List<Object>>();
        findBatchInfo.setInfos("");
        //组装查询数据
        FindCollectInfo findCollect = new FindCollectInfo();
        findCollect.setCusProperty(findBatchInfo.getCusProperty());
        findCollect.setInfo(findBatchInfo.getInfo());
        findCollect.setKeyWord(findBatchInfo.getKeyWord());
        findCollect.setRiskType(findBatchInfo.getRiskType());
        String startTime = findBatchInfo.getStartTime()==null?"":DateUtils.format(findBatchInfo.getStartTime(),DateUtils.DATESHOWFORMAT);
        String endTime = findBatchInfo.getEndTime()==null?"": DateUtils.format(findBatchInfo.getEndTime(),DateUtils.DATESHOWFORMAT);
        findCollect.setStartTime(startTime);
        findCollect.setEndTime(endTime);
        try{
                Validate.validateObject(findBatchInfo);
                //调用接口请求数据
                ResponseModel<CollectTentList> response= riskBatchInterface.riskInfoSummaryQueryInit(findCollect);
                if(!response.isSuccess()){
                    log.error("汇总查询数据失败！",response.getErrorMsg());
                    responseDTO.setSuccess(false);
                    responseDTO.setMsg(response.getErrorMsg());
                    return responseDTO;
                }
                CollectTentList collectTentList = response.getResult();
                List<CollectTent> list = collectTentList.getCollectTent();
                responseDTO.setSumObject(list);
                responseDTO.setSuccess(true);
        }catch (BusiException e){
            log.error("查询参数格式不正确！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getDesc());
        }catch (Exception e){
            log.error("汇总查询数据失败！", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("汇总查询数据失败");
        }
        return responseDTO;
    }
}
